package com.LeeCode;

/**
 * 适龄的朋友
 */

public class Code825 {
    public static void main(String[] args) {
        int[] ages = {108, 115, 5, 24, 82};
        System.out.println(new Code825().numFriendRequests(ages));
    }

    public int numFriendRequests(int[] ages) {
        int[] cnt = new int[121];
        for (int age : ages) {
            cnt[age]++;
        }

        int ageY = 0, ans = 0;
        int count = 0;

        for (int ageX = 0; ageX < 121; ageX++) {
            count += cnt[ageX];
            if (ageY * 2 <= ageX + 14) { // 不能发送好友请求
                count -= cnt[ageY];
                ageY++;
            }
            if (count > 0) { // 存在可以发送好友请求的用户
                ans += cnt[ageX] * count - cnt[ageX];
            }
        }

        return ans;
    }
}
